package com.hc.service.impl;

import com.hc.entity.Role;

import java.util.ArrayList;
import java.util.List;
import com.github.pagehelper.PageHelper;
import com.hc.service.RoleService;
import com.hc.dao.RoleDao;
import com.hc.utils.PageBean;
import com.hc.utils.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public int addRole(Role role){
        int res = roleDao.insertRole(role);
        return res;
    }

    @Override
    public int addRoleSelective(Role role){
        int res = roleDao.insertRoleSelective(role);
        return res;
    }

	@Override
    public int batchAddRole(List<Role> roleList){
		int res = roleDao.batchInsertRole(roleList);
		return res;
	}

    @Override
    public int deleteRoleById(Integer id){
        int res = roleDao.deleteRoleById(id);
        return res;
    }
    
 	@Override
	public int deleteRoleByCondition(Role role){
	 	int res = roleDao.deleteRoleByCondition(role);
        return res;
	}
	
	@Override
	public int batchDeleteRoleByIdList(List<Integer> idList){
		int res = roleDao.batchDeleteRoleByIdList(idList);
        return res;
	}
	
    @Override
    public int updateRole(Role role){
        int res = roleDao.updateRole(role);
        return res;
    }

    @Override
    public int updateRoleSelective(Role role){
        int res = roleDao.updateRoleSelective(role);
        return res;
    }

    @Override
    public long getCount(){
        long res = roleDao.selectCount();
        return res;
    }

    @Override
    public long getCountByCondition(Role role){
        long res = roleDao.selectCountByCondition(role);
        return res;
    }

    @Override
    public Role getRoleById(Integer id){
        Role res = roleDao.selectRoleById(id);
        return res;
    }

    @Override
    public List<Role> getAllRole(){
        List<Role> res = roleDao.selectAllRole();
        return res;
    }

	@Override
	public 	List<Role> getRoleByCondition(Role role){
		List<Role> res = roleDao.selectRoleByCondition(role);
		return res;
	}
	
    @Override
    public PageBean<Role> getRoleWithPagination(PageParam pageParam){
        long count = roleDao.selectCount();
        PageBean<Role> pageBean = new PageBean<>(pageParam, (int)count);
        pageBean.setPageNum(pageParam.getPage());
        PageHelper.startPage(pageParam.getPage(),pageParam.getRows());
        List<Role> data = roleDao.selectAllRole();
        pageBean.setRecords(data);
        return pageBean;
    }

	@Override
	public PageBean<Role> getRoleWithPaginationByCondition(PageParam pageParam, Role role){
        long count = roleDao.selectCountByCondition(role);
        PageBean<Role> pageBean = new PageBean<>(pageParam, (int)count);
        pageBean.setPageNum(pageParam.getPage());
        PageHelper.startPage(pageParam.getPage(),pageParam.getRows());
		List<Role> data = roleDao.selectRoleByCondition(role);
		pageBean.setRecords(data);
        return pageBean;
	}

    //////////////////////////// 外键相关 /////////////////////////////////////
    @Override
    public List<Role> getRoleByParent(Integer parent){
        List<Role> res = roleDao.selectRoleByParent(parent);
        return res;
    }

    @Override
    public List<Role> selectParentRoleByChildId(Integer id) {
        List<Role> parentRoleList = new ArrayList<>();

        Role role = null;

        while((role = roleDao.selectParentRoleByChildId(id)) !=null){
            parentRoleList.add(role);
            id =role.getId();
        }

        return parentRoleList;
    }

}